shift = [[-1, 0], [0, -1], [1, 0], [0, 1]]
def island(grid, visited, x, y):
    for i, j in shift:
        next_x = x + i
        next_y = y + j
        
        if next_x < 0 or next_x >= len(grid[0]) or next_y<0 or next_y>=len(grid):
            continue
        
        if grid[next_y][next_x] == 1 and not visited[next_y][next_x]:
            visited[next_y][next_x] = True
            island(grid, visited, next_x, next_y)
            
if __name__ == '__main__':
    
    row, col = map(int, input().split())
    grid = []
    for i in range(row):
        grid.append(list(map(int, input().split())))

    result = 0
    visited = [[False] * col for _ in range(row)]
    for i in range(row):
        for j in range(col):
            if not visited[i][j] and grid[i][j] == 1:
                result += 1
                visited[i][j] = True
                island(grid, visited, j, i)
    
    print(result)
                